package com.tripcube.file.controller;

import com.tripcube.common.core.web.controller.BaseController;
import com.tripcube.common.core.web.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.tripcube.common.core.domain.R;
import com.tripcube.file.service.ISysFileInfoService;
import com.tripcube.system.api.domain.SysFile;

import java.util.List;

/**
 * 文件请求处理
 *
 * @author alander110
 */
@RestController
@RequestMapping("/file")
public class SysFileController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private ISysFileInfoService sysFileInfoService;

    /**
     * 文件上传请求
     */
    @PostMapping("/upload")
    public R<SysFile> upload(@RequestParam("file") MultipartFile file)
    {
        try
        {
            // 上传并保存文件信息
            SysFile sysFile = sysFileInfoService.uploadFileAndSave(file);
            return R.ok(sysFile);
        }
        catch (Exception e)
        {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量上传文件
     */
    @PostMapping("/uploads")
    public R<List<SysFile>> uploads(@RequestParam("files") MultipartFile[] files){
        try
        {
            List<SysFile> sysFileList = sysFileInfoService.uploadFilesAndSave(files);
            log.info("批量上传文件成功");
            return R.ok(sysFileList);
        }
        catch (Exception e)
        {
            log.error("批量上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 文件删除请求
     */
    @DeleteMapping("/delete/{fileId}")
    public R<Boolean> delete(@PathVariable Long fileId)
    {
        try
        {
            sysFileInfoService.deleteFileAndRecord(fileId);
            log.info("删除文件成功");
            return R.ok(true);
        }
        catch (Exception e)
        {
            log.error("删除文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除文件
     */
    @DeleteMapping("/delete")
    public R<Boolean> delete(@RequestBody Long[] fileIds)
    {
        try
        {
            for (Long fileId : fileIds)
            {
                sysFileInfoService.deleteFileAndRecord(fileId);
            }
            log.info("批量删除文件成功");
            return R.ok(true);
        }
        catch (Exception e)
        {
            log.error("批量删除文件失败", e);
            return R.fail(e.getMessage());
        }
    }


    /**
     * 根据文件ID获取文件信息
     */
    @GetMapping("/info/{fileId}")
    public R<SysFile> getInfo(@PathVariable Long fileId)
    {
        try
        {
            SysFile sysFile = sysFileInfoService.selectSysFileById(fileId);
            if (sysFile == null)
            {
                return R.fail("文件不存在");
            }
            return R.ok(sysFile);
        }
        catch (Exception e)
        {
            log.error("获取文件信息失败", e);
            return R.fail(e.getMessage());
        }
    }


    /**
     * 获取文件列表
     */
    @GetMapping("/list")
    public R<TableDataInfo> list(@RequestBody SysFile sysFile,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize)
    {
        try
        {
            startPage();
            List<SysFile> sysFileList = sysFileInfoService.selectSysFileList(sysFile,pageNum,pageSize);
            log.info("获取文件列表成功");
            TableDataInfo info = getDataTable(sysFileList);
            return R.ok(info);
        }
        catch (Exception e)
        {
            log.error("获取文件列表失败", e);
            throw new RuntimeException(e.getMessage());
        }
    }
}
